package main;

import javax.media.*;
import javax.media.bean.playerbean.MediaPlayer;
import javax.media.pim.PlugInManager;
import javax.swing.JFrame;
import javax.swing.JPanel;

import net.sourceforge.jffmpeg.AudioDecoder;
import net.sourceforge.jffmpeg.VideoDecoder;
import net.sourceforge.jffmpeg.codecs.audio.vorbis.VorbisDecoder;

import java.io.File;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
public class TrivialJMFPlayer extends JFrame {
 
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static void main (String[] args) {
        try {
            JFrame f = new TrivialJMFPlayer();
            f.setPreferredSize(new Dimension(700,500));
            f.pack();
            f.setVisible (true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void addCodec(){
    	Codec vorbis;
    	Codec video;
    	Codec audio;
		String JFFMPEG_VIDEO = "net.sourceforge.jffmpeg.VideoDecoder";
			String JFFMPEG_AUDIO = "net.sourceforge.jffmpeg.AudioDecoder";
			String VORBIS_DECODER = "net.sourceforge.jffmpeg.codecs.audio.vorbis.VorbisDecoder";
			video = new VideoDecoder();
			javax.media.PlugInManager.addPlugIn( JFFMPEG_VIDEO,
			    	video.getSupportedInputFormats(),
			    	video.getSupportedInputFormats(),
			    	PlugInManager.CODEC );
			audio = new AudioDecoder();
			javax.media.PlugInManager.addPlugIn( JFFMPEG_AUDIO,
					audio.getSupportedInputFormats(),
					audio.getSupportedInputFormats(),
			    	PlugInManager.CODEC);
			vorbis = new VorbisDecoder();
			javax.media.PlugInManager.addPlugIn( VORBIS_DECODER,
					vorbis.getSupportedInputFormats(),
					vorbis.getSupportedInputFormats(),
			    	PlugInManager.CODEC);
	}
    
    public TrivialJMFPlayer()throws java.io.IOException,
    java.net.MalformedURLException,
    javax.media.MediaException{
    	JPanel panel = new JPanel();
    	addCodec();
    	//new MediaPlayer();
        FileDialog fd = new FileDialog
            (this, "TrivialJMFPlayer", FileDialog.LOAD);
        fd.setVisible(true);
        File f = new File(fd.getDirectory(), fd.getFile());
        final Player p = Manager.createRealizedPlayer
            (f.toURI().toURL());
        p.setRate(23);
        addWindowListener( new WindowAdapter() {

            public void windowClosing( WindowEvent e ) {

                p.stop();

                p.deallocate();

                p.close();

                System.exit(0);

            }

        });
        panel.setLayout(new FlowLayout(FlowLayout.LEADING,200,20));
        Component visual = p.getVisualComponent();
        Component controller = p.getControlPanelComponent();//VisualComponent();
        controller.setPreferredSize(new Dimension(350,20));
        panel.add(visual);//,FlowLayout.TRAILING);
        panel.add(controller);//,FlowLayout.LEADING);
        add(panel);
        p.start();
    }
}

